<template>
  <div class="app-container" v-loading="loading">
    <customerInformation v-model="customerList" :type="$route.query.type" />
    <statementOfWriteOff v-model="form" :type="$route.query.type" ref="statementOfWriteOff" state='write' />

    <div style="text-align: right;box-sizing: border-box;padding-right: 20px;margin-top: 20px; background-color: #fff;width: 100%;height: 60px;line-height: 60px;border-radius: 8px;">
      <el-button @click="cancel">取消</el-button>
      <el-button @click="sure" type="primary">确定</el-button>
    </div>
  </div>
</template>

<script>
import customerInformation from "../components/customerInformation";
import statementOfWriteOff from "../components/statementOfWriteOff";
import { getWriteOffApplication, saveWriteOffApplication } from "@/api/collectionModule";
export default {
  // 申请销账页面
  name: 'ApplyForWriteOff',
  components: {
    customerInformation,
    statementOfWriteOff
  },
  data() {
    return {
      form: {},
      loading: false,
      customerList: []
    };
  },
  created() {
    this.getWriteOffApplication()
  },
  methods: {
    async getWriteOffApplication() {
      this.loading = true
      try {
        let res = await getWriteOffApplication(this.$route.query.id)
        this.$set(this.form, 'manageId', this.$route.query.id)
        this.$set(this.form, 'amount', res.data.totalOfCanWritableAmount)
        this.loading = false
        this.customerList = [{ customer: res.data.customer, overdueInformation: res.data.overdueInformation, code: res.data.code, totalOfReceivableAmount: res.data.totalOfReceivableAmount, totalOfWaitAmount: res.data.totalOfWaitAmount, totalOfReceivedAmount: res.data.totalOfReceivedAmount, totalWriteOffAmount: res.data.totalWriteOffAmount, totalOfThisMonth: res.data.totalOfThisMonth, totalOfToDay: res.data.totalOfToDay, collectionProgress: res.data.collectionProgress, totalOfCanWritableAmount: res.data.totalOfCanWritableAmount }]
      } catch (error) { }
    },
    cancel() {
      this.$confirm('取消不保存最新数据', '提示', {
        confirmButtonText: '确定取消',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$tab.closeOpenPage(this.$route);
        this.$router.push({
          name: this.$route.query.beFrom,
        });
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消'
        });
      });
    },
    sure() {
      let isValidate = this.$refs.statementOfWriteOff.validate()
      if (isValidate) {
        this.$confirm('确认提交销账申请？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.saveWriteOffApplication()
        }).catch((err) => { console.log(err); });
      } else {
        this.$nextTick(() => {
          const el = document.querySelectorAll(".el-form-item__error")[0];
          el.scrollIntoView({ block: "center", behavior: "smooth" });
        });
      }
    },
    // ** 销账申请提交接口 ** //
    saveWriteOffApplication() {
      saveWriteOffApplication(this.form).then(res => {
        this.$message.success('提交成功');
        // 通过走接口
        this.$tab.closeOpenPage(this.$route);
        this.$router.push({
          name: this.$route.query.beFrom,
        });
      })
    },
  },
};
</script>
<style lang="scss" scoped>
.app-container {
  overflow: auto !important;
}
</style>
